// pages/mian-video/main-video.js
import {
  getToMV
} from '../../services/video'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    videoList: [],
    offset: 0,
    hasMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetchTopMV()
  },

  // 网络请求
  async fetchTopMV() {
    const result = await getToMV(20, this.data.offset)

    const newVideoList = [...this.data.videoList, ...result.data]
    this.setData({
      videoList: newVideoList
    })
    this.data.offset = this.data.videoList.length
    this.data.hasMore = result.hasMore


  },

  // 监听上拉和下拉功能
  onReachBottom() {
    // 1.判断是否有更多数据
    if (!this.data.hasMore) return
    // 2.如果有更多数据，在请求新的数据
    this.fetchTopMV()
  },
  // 上拉刷新
  async onPullDownRefresh() {
    // 1.清空之前数据
    this.setData({
      videoList: []
    })
    this.data.offset = 0
    this.data.hasMore = true
    await this.fetchTopMV()
    wx.stopPullDownRefresh()
  },

  // 界面事件监听的方法
  onVideoItemTap(event) {
    const item = event.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/detail-video/detail-video?id=${item.id}`,
    })
  }
})